package com.oneday.ashop.webgate.base.ftp;

import org.apache.commons.net.ftp.FTPClient;
import org.springframework.web.multipart.MultipartFile;
import sun.net.ftp.FtpClient;

import java.io.IOException;

/**
 * ftp工具类
 * User:bob.yang
 * Date 2015/8/18 0018
 */
public class FtpUtil {

    private static String ip = "139.129.12.155";
    private static int port = 20;

    private static String username = "root";
    private static String password = "gaoyang520884";
    private static String path = "/media/image/";

    /**
     * 上传文件
     * @param file
     */
    public static void upload(MultipartFile file) {
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(ip, port);
            ftpClient.login(username, password);
            ftpClient.changeWorkingDirectory(path);
            ftpClient.storeFile("myloveGirl", file.getInputStream());
            file.getInputStream().close();
            ftpClient.logout();
            ftpClient.disconnect();
        } catch (IOException e) {
            try {
                ftpClient.disconnect();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
    }

}
